<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="com.eamoi.ajax.*"%>
<%
BlogService service = new BlogService();
Blog[] bloges = service.getAllBlog();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ch11--模拟MSN Space</title>
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../function/ajax_func.js"></script>
<script language="javascript">
var blogid = "";
//"阅读评论"按钮事件
function viewComment(id) {
	blogid = id;
	send_request("GET","../msnspace/blogManager?action=viewComment&blogid="+id,null,"XML",populateComment);
}
//"添加评论"按钮事件
function addComment(id) {
	blogid = id;
	send_request("GET","../msnspace/blogManager?action=viewComment&blogid="+id,null,"XML",populateComment);
}
//发表评论
function submitComment(id) {
	var formName = id+".commentForm";
	var f = document.getElementById(formName);
	var str = "author="+f.author.value+"&";
	str += "email="+f.email.value+"&";
	str += "blogurl="+f.blogurl.value+"&";
	str += "commentcontent="+f.commentcontent.value;
	send_request("POST","../msnspace/blogManager?action=addComment&blogid="+id,str,"XML",populateComment);
}
//回调函数，显示评论列表及其表单
function populateComment() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			//alert(http_request.responseXML);
			//alert("XML文档响应。");
			var doc = http_request.responseXML;
			var innerHTML = "";
			var commentes = doc.getElementsByTagName("comment");
			if(commentes.length!=0) {
				innerHTML = "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">";
				innerHTML += "<tr><td width=\"100\">评论</td>";
				innerHTML += "<td align=\"right\">上一页|下一页|<a href=\"javascript:void(0)\" onClick=\"closeComment('"+blogid+"')\">关闭</a>&nbsp;&nbsp;</td>";
				innerHTML += "</tr></table>";
				innerHTML += "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">";
				document.getElementById(blogid+".commenttotal").innerHTML = commentes.length;
				for(var i=0;i<commentes.length;i++) {
					var author = (commentes[i].childNodes[0].firstChild==null)?"":commentes[i].childNodes[0].firstChild.data;
					var blogimage = (commentes[i].childNodes[1].firstChild==null)?"../images/blogimage.jpg":commentes[i].childNodes[1].firstChild.data;
					var commentdate = (commentes[i].childNodes[2].firstChild==null)?"":commentes[i].childNodes[2].firstChild.data;
					var blogurl = (commentes[i].childNodes[3].firstChild==null)?"":"（"+commentes[i].childNodes[3].firstChild.data+"）";
					var email = (commentes[i].childNodes[4].firstChild==null)?"":commentes[i].childNodes[4].firstChild.data;
					var commentcontent = (commentes[i].childNodes[5].firstChild==null)?"":commentes[i].childNodes[5].firstChild.data;
					innerHTML += "<tr><td bgcolor=\"#FFFFFF\" height=\"25\">";
					innerHTML += "<img src=\""+blogimage+"\" width=\"30\" height=\"30\" border=\"0\" align=\"absmiddle\">";
					innerHTML += commentcontent;
					innerHTML += "<br><br>发布者&nbsp;<strong>"+author+blogurl+"</strong>-"+commentdate;
					innerHTML += "</td></tr>";
				}
				innerHTML += "</table>";
			}
			else {
				//innerHTML += "<tr><td bgcolor=\"#FFFFFF\" height=\"25\"></td></tr>";
			}
			document.getElementById(blogid+".commentList").innerHTML = makeCommentForm(innerHTML,blogid);
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
//组织评论表单的HTML代码
function makeCommentForm(commentList,blogid) {
	var html = "<form name=\""+blogid+".commentForm\" id=\""+blogid+".commentForm\" action=\"../msnspace/blogManager?action=addComment&blogid="+blogid+"\" method=\"post\">";
	html += commentList;
	html += "<table width=\"500\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">\n";
	if(commentList!="") {
		html += "<tr><td bgcolor=\"#1E6E0D\">发布评论</td></tr>\n";
		html += " <tr> <td bgcolor=\"#FFFFFF\">";
	}
	else {
		html += "<tr><td bgcolor=\"#1E6E0D\" width=\"100\">发布评论</td><td align=\"right\"><a href=\"javascript:void(0)\" onClick=\"closeComment('"+blogid+"')\">关闭</a>&nbsp;&nbsp;</td></tr>\n";
		html += " <tr> <td bgcolor=\"#FFFFFF\" colspan=\"2\">";

	}
	html += " <table width=\"100%\"  border=\"0\" cellpadding=\"4\" cellspacing=\"0\">\n";
	html += "<tr><td width=\"19%\" align=\"center\">";
	html += "<img src=\"../images/blogimage.jpg\" width=\"30\" height=\"30\" border=\"0\" align=\"absmiddle\"></td>\n";
	html += " <td width=\"81%\"><input name=\"archive\" type=\"checkbox\" id=\"archive\" value=\"true\" checked>\n";
	html += "使用我的档案文件信息</td></tr>\n";
	html += " <tr> <td colspan=\"2\">名称：<br>\n";
	html += "<input name=\"author\" type=\"text\" class=\"form_text\" id=\"author\" size=\"80\"></td> </tr>\n";
	html += " <tr> <td colspan=\"2\">电子邮件（可选）：<br>\n";
	html += " <input name=\"email\" type=\"text\" class=\"form_text\" id=\"email\" size=\"80\"></td> </tr>\n";
	html += " <tr> <td colspan=\"2\">您的网络日志URL（可选）：<br>\n";
	html += "<input name=\"blogurl\" type=\"text\" class=\"form_text\" id=\"author3\" size=\"80\"></td> </tr>\n";
	html += "<tr> <td colspan=\"2\">评论：<br>\n";
	html += "<textarea name=\"commentcontent\" cols=\"78\" rows=\"6\" class=\"form_textarea\"></textarea></td> </tr>\n";
	html += " <tr align=\"center\"> <td colspan=\"2\">\n";
	html += "<input name=\"Submit\" type=\"button\" class=\"button\" value=\" 评论 \" onClick=\"submitComment('"+blogid+"')\"></td>\n";
	html += "</tr> </table></td> </tr></table></form>\n";
	return html;
}
//关闭评论窗口
function closeComment(id) {
	document.getElementById(blogid+".commentList").innerHTML = "";
}
//"引用通告"事件函数
function viewReference(id) {
	blogid = id;
	send_request("GET","../msnspace/blogManager?action=viewReference&blogid="+id,null,"XML",populateReference);
}
//"引用通告"回调函数
function populateReference() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			var doc = http_request.responseXML;
			var track = doc.getElementsByTagName("track")[0].firstChild.data;
			var innerHTML = "";
			innerHTML = "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">";
			innerHTML += "<tr><td width=\"100\">引用通告</td>";
			innerHTML += "<td align=\"right\"><a href=\"javascript:void(0)\" onClick=\"closeReference('"+blogid+"')\">关闭</a>&nbsp;&nbsp;</td>";
			innerHTML += "</tr></table>";
			innerHTML += "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">";
			innerHTML += "<tr bgcolor=\"#FFFFFF\"><td height=\"25\">";
			innerHTML += "此项的引用通告URL是：<br>"+track+"<br>";
			innerHTML += "引用此项的网络日志<ul>";
			var references = doc.getElementsByTagName("reference");
			if(references.length!=0) {
				for(var i=0;i<references.length;i++) {
					var refername = (references[i].childNodes[0].firstChild==null)?"":references[i].childNodes[0].firstChild.data;
					var referurl = (references[i].childNodes[1].firstChild==null)?"":references[i].childNodes[1].firstChild.data;
					innerHTML += "<li><a href=\""+referurl+"\" target=\"_blank\">"+refername+"</a></li>";
				}
			}else {
				innerHTML += "<li>无</li>";
			}
			innerHTML += "</ul></td></tr>";
			innerHTML += "</table>";
			document.getElementById(blogid+".reference").innerHTML = innerHTML;
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
function closeReference(id) {
	document.getElementById(id+".reference").innerHTML = "";
}
//"固定链接"事件函数
function viewEntry(id) {
	blogid = id;
	send_request("GET","../msnspace/blogManager?action=viewEntry&blogid="+id,null,"Text",populateEntry);
}
function populateEntry() {
	if (http_request.readyState == 4) { // 判断对象状态
		if (http_request.status == 200) { // 信息已经成功返回，开始处理信息
			var entry = http_request.responseText;
			var innerHTML = "";
			innerHTML = "<table width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#1E6E0D\">";
			innerHTML += "<tr><td width=\"100\">固定链接</td>";
			innerHTML += "<td align=\"right\"><a href=\"javascript:void(0)\" onClick=\"closeEntry('"+blogid+"')\">关闭</a>";
			innerHTML += "&nbsp;&nbsp;</td></tr>";
			innerHTML += "<tr><td bgcolor=\"#FFFFFF\" height=\"25\" colspan=\"2\"><a href=\""+entry+"\" target=\"_blank\">"+entry+"</a></td></tr>";
			innerHTML += "</table>";		
			document.getElementById(blogid+".entry").innerHTML = innerHTML;
		} else { //页面不正常
			alert("您所请求的页面有异常。");
		}
	}
}
function closeEntry(id) {
	document.getElementById(id+".entry").innerHTML = "";
}
</script>
</head>

<body bgcolor="#B2E392"><center>
<%
if(bloges!=null) {
	for(int i=0;i<bloges.length;i++) {
%>
	<table width="500" border="0" cellspacing="0" cellpadding="0">
			<tr>
					<td><%=bloges[i].getPublishdate()%></td>
			</tr>
	</table>
	<hr width="500" size="1" noshade color="#666666">
	<table width="500" border="0" cellspacing="0" cellpadding="0">
			<tr>
					<td><strong><%=bloges[i].getTitle()%></strong></td>
			</tr>
	</table>
	<table width="500" border="0" cellspacing="0" cellpadding="0">
			<tr>
					<td><%=bloges[i].getContent()%></td>
			</tr>
	</table>
	<%
		BlogImage[] images = (BlogImage[])bloges[i].getImages().toArray(new BlogImage[0]);
		if((images!=null)&&(images.length!=0)) {
			out.println("<table width='500' border='0' cellspacing='0' cellpadding='0'>");
			for(int j=0;j<images.length;j++) {
				if(j%2==0) out.println("<tr><td>");
				out.println("<a href='"+images[i].getUrl()+"' target='_blank'>");
				out.println("<img src='"+images[i].getUrl()+"' border='0' width='96' height='72' alt='"+images[i].getImageName()+"'>");
				out.println("</a>");
				if(j%2==0) out.println("</td></tr>");
			}
			out.println("</table>");
		}
	%>
	<table width="500" border="0" cellspacing="0" cellpadding="0">
			<tr>
					<td>
					<a href="javascript:void(0)" onClick="addComment('<%=bloges[i].getId()%>')">添加评论</a>|
					<a href="javascript:void(0)" onClick="viewComment('<%=bloges[i].getId()%>')">阅读评论（<label id="<%=bloges[i].getId()%>.commenttotal"><%=bloges[i].getCommenttotal()%></label>）</a>
					</td>
			</tr>
	</table>
	<table width="500" border="0" cellspacing="0" cellpadding="0">
			<tr>
					<td>
					<%=bloges[i].getPublishdate().substring(11,bloges[i].getPublishdate().length())%>|
					<a href="javascript:void(0)" onClick="viewEntry('<%=bloges[i].getId()%>')">固定链接</a>|
					<a href="javascript:void(0)" onClick="viewReference('<%=bloges[i].getId()%>')">引用通告（<%=bloges[i].getReferencetotal()%>）</a>|
					记录它|<%=bloges[i].getCatalogue()%>
					</td>
			</tr>
	</table>
	<table width="500" border="0" cellspacing="0" cellpadding="0">
		<tr>
			<td>
			<label id="<%=bloges[i].getId()%>.commentList"></label>
			<label id="<%=bloges[i].getId()%>.entry"></label>
			<label id="<%=bloges[i].getId()%>.reference"></label>
			</td>
		</tr>
	</table>
<%
	}
}
%>
</center>
</body>
</html>
